import { Component, Input, Output, EventEmitter } from '@angular/core';
import { SinglePwdData } from "../singlepwd/singlepwd.component";
@Component({
    selector: 'passwords',
    styleUrls: ['./passwords.component.css'],
    templateUrl: './passwords.component.html'
})
export class PasswordComponent {
    private singlePwdDatas: Array<SinglePwdData> = [];
    private ckMsg: string = '显示密码';
    private info: Array<string> = ['弱', '中', '强'];
    private ckShowFlg: boolean = false;
    private regs: RegExp[] = [
        new RegExp(/[0-9]/),
        new RegExp(/[a-zA-Z]/),
        new RegExp(/[\@\#\$\%\^\&\*\(\)\_\+\!]/),
    ];
    @Input()
    private set pwdData(str: string) {
        let s: number = 0;
        if (str.length > 6) {
            for (let reg of this.regs) {
                reg.test(str) && s++;
            }
        }
        this.setSinglePwdDatas(s);
    }
    @Output()
    private isShow: EventEmitter<boolean>;

    constructor() {
        this.isShow = new EventEmitter<boolean>();
    }

    private setSinglePwdDatas(flg: number = 0): void {

        this.singlePwdDatas = [];

        for (let i = 0; i < 3; i++) {
            this.singlePwdDatas.push({ title: this.info[i], isCheck: i < flg });
        }
    }

    private isShowPwd(): void {
        this.ckShowFlg = !this.ckShowFlg;
        this.ckMsg = this.ckShowFlg ? '隐藏密码' : '显示密码';
        this.isShow.emit(this.ckShowFlg);
    }

}